package com.wuliu.common.utils;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.wuliu.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 快宝工具类
 *
 * @author realue
 * @version 1.0
 * @date 2023-10-27 16:45
 */
@Slf4j
public class KuaidiHelpUtil {
    private static final String KUAIDIHELP_METHOD = "cloud.address.resolve";

    public static Map<String, Object> geocodingAddress(String content) throws Exception {
        Map<String, Object> retMap = new HashMap<>();
        String appId = DictUtils.getDictValue("kuaidi_help", "app_id");
        String apiKey = DictUtils.getDictValue("kuaidi_help", "app_key");
        String url = DictUtils.getDictValue("kuaidi_help", "url");
        String path = "/api";
        String ts = System.currentTimeMillis() + "";
        Map<String, String> headers = new HashMap<>();
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> bodys = new HashMap<>();
        bodys.put("app_id", appId);
        bodys.put("method", KUAIDIHELP_METHOD);
        bodys.put("ts", ts);
        String sign = DigestUtil.md5Hex(appId + KUAIDIHELP_METHOD + ts + apiKey);
        bodys.put("sign", sign);
        Map<String, String> data = new HashMap<>(2);
        data.put("multimode", "false");
        data.put("text", content);
        bodys.put("data", JSONObject.toJSONString(data));
        log.info("params={}", JSONObject.toJSONString(bodys));
        HttpResponse response = HttpUtils.doPost(url, path, "POST", headers, null, bodys);
        String responseStr = EntityUtils.toString(response.getEntity());
        log.info("responseStr={}", responseStr);
        Map<String, Object> map = JSONUtil.toBean(responseStr, Map.class);
        if (StringUtils.isNotEmpty(map) && map.containsKey("code") && (int) map.get("code") == 0 && map.containsKey("data")) {
            List<Map<String, String>> datas = (List<Map<String, String>>) map.get("data");
            if (StringUtils.isNotEmpty(datas)) {
                Map<String, String> dataMap = datas.get(0);
                String tel = MapUtils.getString(dataMap, "phone");
                String mobile = MapUtils.getString(dataMap, "mobile");
                String sendName = MapUtils.getString(dataMap, "name");
                String detailAddr = MapUtils.getString(dataMap, "detail");
                String province = MapUtils.getString(dataMap, "province_name");
                String city = MapUtils.getString(dataMap, "city_name");
                String county = MapUtils.getString(dataMap, "county_name");
                retMap.put("tel", StringUtils.isEmpty(mobile) ? tel : mobile);
                retMap.put("sendName", sendName);
                retMap.put("detailAddr", detailAddr);
                retMap.put("province", province);
                retMap.put("city", city);
                retMap.put("county", county);
            }
        }
        return retMap;
    }
}
